/**
 * 
 */
package com.mzba.place.adapter;

import java.text.DecimalFormat;
import java.util.List;

import android.R.integer;
import android.content.Context;
import android.content.res.Resources;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.mzba.place.R;
import com.mzba.place.entity.CoordinateEntity;
import com.mzba.place.entity.ReportEntity;
import com.mzba.place.utils.ImageLoader;
import com.mzba.place.utils.StringUtil;
import com.mzba.place.utils.Utils;

/**
 * 列表适配器
 * 
 * @author iris
 * 
 */
public class ReportListAdapter extends BaseAdapter {

	private List<ReportEntity> reportList;
	private LayoutInflater layoutInflater;
	private String[] reportType;
	private int[] reportTypeIcon;
	ImageLoader mImageLoader;
	private double localLat;
	private double localLon;
	private Resources resources;
	String distances;
	private final double EARTH_RADIUS = 6378137.0;

	private String km, meter;
	DecimalFormat df;

	public ReportListAdapter(Context context, List<ReportEntity> reportList,
			double localLat, double localLon, ImageLoader mImageLoader) {
		this.reportList = reportList;
		this.localLat = localLat;
		this.localLon = localLon;
		this.mImageLoader = mImageLoader;
		layoutInflater = LayoutInflater.from(context);
		reportType = Utils.getReportType(context);
		reportTypeIcon = Utils.getReportTypeIcon();
		resources = context.getResources();
		km = resources.getString(R.string.km);
		meter = resources.getString(R.string.meter);
		df = new DecimalFormat("########0.00");
	}

	@Override
	public int getCount() {
		return reportList == null ? 0 : reportList.size();
	}

	@Override
	public Object getItem(int arg0) {
		return reportList == null ? null : reportList.get(arg0);
	}

	@Override
	public long getItemId(int position) {
		return reportList.get(position).getId();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;

		if (convertView == null) {
			convertView = layoutInflater.inflate(
					R.layout.fragment_report_list_item, null);
			holder = new ViewHolder();
			holder.tv_report_type = (TextView) convertView.findViewById(R.id.tv_report_type);
			holder.tv_report_comment = (TextView) convertView.findViewById(R.id.tv_report_comment);
			holder.iv_report_image = (ImageView) convertView.findViewById(R.id.iv_report_image);
			holder.tv_report_type = (TextView) convertView.findViewById(R.id.tv_report_type);
			holder.tv_report_time = (TextView) convertView.findViewById(R.id.tv_report_time);
			holder.tv_report_address = (TextView) convertView.findViewById(R.id.tv_report_address);
			holder.tv_report_recording = (ImageView) convertView.findViewById(R.id.iv_report_recording);
			holder.tv_report_content = (TextView) convertView.findViewById(R.id.tv_report_content);
			holder.iv_report_type_icon = (ImageView) convertView.findViewById(R.id.tv_report_icon);
			holder.tv_report_vote = (TextView) convertView.findViewById(R.id.tv_report_vote);
			convertView.setTag(holder);

		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		try {
			ReportEntity reportEntity = reportList.get(position);
			CoordinateEntity coordinateEntity = reportEntity.getCoordinate();
			if (reportEntity.getType_id() != -1) {
				holder.tv_report_type.setText(reportType[reportEntity
						.getType_id()] + "");
				holder.iv_report_type_icon
						.setBackgroundResource(reportTypeIcon[reportEntity
								.getType_id() - 1]);
			}
			holder.tv_report_content.setText(reportEntity.getText() + "");
			holder.tv_report_time.setText(Utils.getCreateAt(reportEntity
					.getCreated_at()));
			if (StringUtil.isBlank(reportEntity.getThumbnail_pic())) {
				holder.iv_report_image.setVisibility(View.GONE);
			} else {
				holder.iv_report_image.setVisibility(View.VISIBLE);
				mImageLoader.addTask(reportEntity.getThumbnail_pic(),
						holder.iv_report_image);
			}
			double distance = getDistance(coordinateEntity.getLat(),
					coordinateEntity.getLon(), localLat, localLon);
			
			if (distance >= 1000) {
				distance = distance / 1000;
				String dis = df.format(distance)+"";
				
				if (dis.endsWith(".0")||dis.endsWith(".00")) {
					distances = String.format(resources.getString(R.string.distance),
							 (int)distance + km);
				}else {
					distances = String.format(resources.getString(R.string.distance),dis+ km);
				}
			} else {
				distances = String.format(resources.getString(R.string.distance),
						 (int) distance + meter);
			}

			holder.tv_report_address.setText(distances + "");
			int votes = reportEntity.getVotes_count();
			if (votes == 0) {
				holder.tv_report_vote.setVisibility(View.INVISIBLE);
			} else {
				holder.tv_report_vote.setVisibility(View.VISIBLE);
				holder.tv_report_vote.setText(votes + "");
			}
			int comment = reportEntity.getComments_count();
			if (comment == 0) {
				holder.tv_report_comment.setVisibility(View.INVISIBLE);
			} else {
				holder.tv_report_comment.setVisibility(View.VISIBLE);
				holder.tv_report_comment.setText(comment + "");
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return convertView;
	}

	static class ViewHolder {
		ImageView iv_report_type_icon;
		ImageView tv_report_recording;
		TextView tv_report_type;
		TextView tv_report_time;
		TextView tv_report_content;
		ImageView iv_report_image;
		TextView tv_report_address;
		TextView tv_report_comment;
		TextView tv_report_vote;
	}

	/**
	 * 计算两个经纬度的距离
	 * 
	 * @param lat_a
	 * @param lng_a
	 * @param lat_b
	 * @param lng_b
	 * @return
	 */
	private double getDistance(double lat_a, double lng_a, double lat_b,
			double lng_b) {
		double radLat1 = (lat_a * Math.PI / 180.0);
		double radLat2 = (lat_b * Math.PI / 180.0);
		double a = radLat1 - radLat2;
		double b = (lng_a - lng_b) * Math.PI / 180.0;
		double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)
				+ Math.cos(radLat1) * Math.cos(radLat2)
				* Math.pow(Math.sin(b / 2), 2)));
		s = s * EARTH_RADIUS;
		s = Math.round(s * 10000) / 10000;
		return s;
	}

	/**
	 * @return the reportList
	 */
	public List<ReportEntity> getReportList() {
		return reportList;
	}
}
